import routes from './routes';

const { NODE_ENV, BASE = '/', WEB_SOCKET_URL = '', DEVTOOL = false } = process.env;

const target =
  NODE_ENV === 'production'
    ? 'https://bom.geelycv.com/'
    : NODE_ENV === 'development'
    ? // 'http://10.30.52.236:9090/'
      'https://bom-dev.geelycv-test.com/'
    : 'https://bom-test.geelycv-test.com/';

const boomTarget =
  NODE_ENV === 'production'
    ? 'https://bom.geelycv.com/'
    : NODE_ENV === 'development'
    ? //'http://10.30.52.236:9090/' // 朱道林
      // 'http://10.30.52.154:9090/' // 来永亮

      //'http://localhost:9090/' // 网关
      'https://bom-dev.geelycv-test.com/' // 测试环境
    : 'https://bom-test.geelycv-test.com/';

// 在 umiConfig 定义之前添加通用的拦截方法
const createBypassFn = () => {
  return function bypass(req: any, res: any, proxyOptions: any) {
    // 如果路径不包含 .api，认为是页面请求
    if (!req.path.includes('.api')) {
      // 再检查是否明显是静态资源请求
      const isStaticRequest = /\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|eot)$/.test(req.path);

      if (!isStaticRequest) {
        return '/index.html';
      }
    }
    // 是 API 请求，继续代理
    return null;
  };
};

const umiConfig = {
  proxy: {
    '/fwPlatform': {
      target,
      changeOrigin: true,
    },
    '/fwFile': {
      target,
      changeOrigin: true,
    },
    '/fwWorkflow': {
      target,
      changeOrigin: true,
    },
    '/fwSecurity': {
      target,
      changeOrigin: true,
    },
    '/fwIntegration': {
      target,
      changeOrigin: true,
    },
    '/ddm': {
      target,
      changeOrigin: true,
    },
    '/mstdata': {
      // target,
      target: boomTarget,
      changeOrigin: true,
      bypass: createBypassFn(),
    },
    '/cfgmgmt': {
      target,
      changeOrigin: true,
    },
    '/chgmgmt': {
      target,
      changeOrigin: true,
    },
    '/bommgmt': {
      // target,
      target: boomTarget,
      changeOrigin: true,
    },
    '/integration': {
      target,
      changeOrigin: true,
    },
  },
  base: BASE,
  publicPath: BASE,
  hash: true,
  antd: {},
  alias: {},
  dva: {
    // 配置umi中使用getDvaApp
    hmr: true,
    disableModelsReExport: true,
    lazyLoad: true,
  },
  externals: {
    // react: 'React',
    // 'react-dom': 'ReactDOM',
    // moment: 'moment',
  },
  devtool: 'inline-source-map', // 或者使用 'cheap-module-source-map', 断点调试
  define: {},
  history: {
    type: 'browser',
    options: {
      fallback: '/index.html',
    },
  },
  theme: {
    'primary-color': '#027aff',
  },
  locale: {
    default: 'zh-CN',
    baseNavigator: true,
  },
  extraBabelPlugins: [
    [
      'import',
      {
        libraryName: 'gantd',
        libraryDirectory: 'lib',
        style: true, // 自动加载 CSS
      },
    ],
  ],
  routes,
  title: false,
  ignoreMomentLocale: true,
  manifest: {
    basePath: '/',
  },
  nodeModulesTransform: {
    type: 'none',
  },
  devServer: {
    hot: true,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': '*',
      'Access-Control-Allow-Headers': '*',
    },
  },
  // 设置基础布局组件
  // layout: {
  //   title: '我的应用',
  //   // 自定义布局组件路径
  //   locale: false,
  //   // 如果需要自定义 Layout 路径
  //   component: '@/layouts/index',
  // },
};

export default umiConfig;
